<template>

    <div> <!--有且只能有一个标签-->
        <el-container>
            <el-header>
                测试数据库读写分离登录注册模块
            </el-header>
            <!--登录表单，含自动校验-->
            <el-main>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">登录</el-button>
                        <el-button type="primary" @click="save()">注册</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>



</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                ruleForm: {
                    username: '',
                    passward: '',
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    passward: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm() {
              
                this.$axios.post("/user/login", this.ruleForm).then(res => { 
                    if(res.data.code == 200){
                        const userInfo = res.data.data
                        console.log(userInfo);
                        // 登录成功跳转到首页
                        this.$router.push("/index") 
                    } else {
                        alert(res.data.msg)
                    }
                })
                   
                
            },
            save(){

                this.$axios.post("/user/save", this.ruleForm).then(res => { 
                    if(res.data.code == 200){
                        const userInfo = res.data.data
                        console.log(userInfo);
                        // 注册成功提示
                        alert("注册成功！")
                    } else {
                        alert(res.data.msg)
                    }
                })

            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .el-header, .el-footer {
        background-color: #B3C0D1;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-aside {
        background-color: #D3DCE6;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        background-color: white;
        color: #333;
        text-align: center;
        line-height: 160px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .demo-ruleForm{
        max-width: 500px;
        margin: 0 auto;
    }

</style>